package com.spring.framwork.springmvc;

import com.spring.framwork.spring.annotation.Autowried;
import com.spring.framwork.spring.annotation.Component;
import com.spring.framwork.spring.annotation.PostConstruct;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.slf4j.bridge.SLF4JBridgeHandler;

import java.io.File;
import java.util.logging.LogManager;

@Component
public class TomcatServicet {
    @Autowried
    private DispatcherServlet dispatcherServlet;
    @PostConstruct
    public void start() throws LifecycleException {
        LogManager.getLogManager().reset();
        SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();
        int port=8080;
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(port);
        tomcat.getConnector();
        String contextPath="";
        String docBase=new File(".").getAbsolutePath();
        Context context=tomcat.addContext(contextPath,docBase);

        tomcat.addServlet(contextPath, "dispatcherServlet", dispatcherServlet);
        context.addServletMappingDecoded("/*","dispatcherServlet");
        tomcat.start();
        System.out.println("tomcat Start 。。。 port"+port);
    }
}
